package com.ldj.hibernate;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.ldj.struts.form.PetitionInfoForm;

public class PetitionInfoDAO extends BaseHibernateDAO {
	public PetitionInfoDAO(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
		// TODO Auto-generated constructor stub
	}

	private static final Log log = LogFactory.getLog(PetitionInfoDAO.class);
	
	public List findAllPage(String userId, PetitionInfoForm pForm) {
		try {
			String queryString = "from PetitionInfo where petitionInfoDelOper='0' ";
			if(userId != null && !"".equals(userId)){
				queryString += " and petitionInfoCreator='" + userId + "'";
			}
			if(pForm.getPetitionInfoRegister() != null && !"".equals(pForm.getPetitionInfoRegister())){
				queryString += " and petitionInfoRegister = '" + pForm.getPetitionInfoRegister() + "'";
			}
			if(pForm.getPetitionInfoType() != null && !"".equals(pForm.getPetitionInfoType())){
				queryString += " and petitionInfoType = '" + pForm.getPetitionInfoType()+ "'";
			}
			queryString += "order by petitionInfoTime desc";
			Query queryObject = getSession().createQuery(queryString);
			this.setPageSize(9);
			return this.jlist(queryObject);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public PetitionInfo findById(java.lang.String id) {
		log.debug("getting PetitionInfo instance with id: " + id);
		try {
			PetitionInfo instance = (PetitionInfo) getSession().get(
					"com.ldj.hibernate.PetitionInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public void save(PetitionInfo transientInstance) {
		log.debug("saving PetitionInfo instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public PetitionInfo merge(PetitionInfo detachedInstance) {
		try {
			PetitionInfo result = (PetitionInfo) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	public List findFromIdArray(String[] id){
		try {
			if(id != null && id.length > 0){
				String idChar = "";
				for(int i = 0; i < id.length; i ++){
					idChar += id[i] + ",";
				}
				idChar = idChar.substring(0, idChar.length() - 1);
				idChar = idChar.replace(",", "','");
				String queryString = "from PetitionInfo where petitionInfoDelOper='0' and petitionInfoId in ('" + idChar + "')";
				Query queryObject = getSession().createQuery(queryString);
				return queryObject.list();
			}
			return null;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	
	
	public void delete(PetitionInfo persistentInstance) {
		log.debug("deleting PetitionInfo instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
}